package demo.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ErrorController {
	
	
	
	@RequestMapping("/commerror")
	public Map<String,Object> error(HttpServletRequest request) throws Throwable{
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("error",request.getAttribute("error_msg"));
		return map;
		
	}
	

	@RequestMapping("/errorTest")
	public String errorTest(HttpServletRequest request) throws Throwable{
		String error = request.getParameter("error");
		if(error!=null){
			error = error.trim();
		}
		if(error==null || error.length()<=0){
			return "run success,"+new Date();
		}
		// Throwable  
		// Error extends Throwable 
		// Exception extends Throwable
		// RuntimeException extends Exception 
		// 
		
		if("Throwable".equalsIgnoreCase(error)){
			throw new Throwable("error,"+new Date());
		}
		

		if("Error".equalsIgnoreCase(error)){
			throw new Error("error,"+new Date());
		}
		

		if("Exception".equalsIgnoreCase(error)){
			throw new Exception("error,"+new Date());
		}

		if("RuntimeException".equalsIgnoreCase(error)){
			throw new RuntimeException("error,"+new Date());
		}
		
		throw new Exception("error,"+new Date());
	}
	
	
}